﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace RaidAssist.DataAccess
{
    /// <summary>
    /// Class that provides an interface onto our database
    /// </summary>
    public class DBInterface
    {
        #region Member variables
        #endregion

        #region Public methods
        /// <summary>
        /// Creates a new user
        /// </summary>
        /// <param name="username">The username</param>
        /// <param name="passwordHash">The hash of their password</param>
        /// <param name="displayName">The display name for the new user</param>
        /// <param name="emailAddress">The email address for the new user</param>
        /// <returns><b>true</b> if the user was created, <b>false if it already exists</b></returns>
        public bool CreateUser(string username,
                               string passwordHash,
                               string displayName,
                               string emailAddress)
        {
            using (SqlConnection conn = this.GetConnection())
            {
                SqlCommand command = conn.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "RA_addUser_sp";
                command.Parameters.AddWithValue("@username", username);
                command.Parameters.AddWithValue("@passwordHash", passwordHash);
                command.Parameters.AddWithValue("@displayName", displayName);
                command.Parameters.AddWithValue("@emailAddress", emailAddress);

                return command.ExecuteNonQuery() > 0;
            }
        }
        #endregion

        #region Private methods
        /// <summary>
        /// Retrieves a new connection to our database and opens it
        /// </summary>
        /// <returns>A connection to our database</returns>
        private SqlConnection GetConnection()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RaidAssistConnectionString"].ConnectionString);
            conn.Open();

            return conn;
        }
        #endregion
    }
}
